import {login, logout, getInfo, getCardLogin} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'
import {encrypt} from '@/utils/jsencrypt'
import Cookies from 'js-cookie'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: window.localStorage.getItem("avatar") || "",
    roles: [],
    permissions: [],
    hasPlatform: [],
    fieldCodes: '',
    customsCode: '',
    nickName: "",
    idCardNo: "",
    userLoginInfo: null,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
      window.localStorage.setItem("avatar", avatar)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_HASPLATFORM: (state, hasPlatform) => {
      state.hasPlatform = hasPlatform
    },
    SET_FIELDCODES: (state, fieldCodes) => {
      state.fieldCodes = fieldCodes
    },
    SET_CUSTOMSCODE: (state, customsCode) => {
      state.customsCode = customsCode
    },
    SET_NICKNAME: (state, nickName) => {
      state.nickName = nickName
    },
    SET_IDCARDNO: (state, idCardNo) => {
      state.idCardNo = idCardNo
    },
    SET_LOGIN_USER: (state, userInfo) => {
      state.userLoginInfo = userInfo
    }
  },

  actions: {
    // 登录
    Login({commit}, loginForm) {
      if (loginForm.password) {
        loginForm.password = encrypt(loginForm.password);
      }
      return new Promise((resolve, reject) => {
        login(loginForm).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve(res)
        })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 卡介质登录
    CardLogin({commit}, clientId) {
      return new Promise((resolve, reject) => {
        getCardLogin(clientId).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve(res)
        })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 获取用户信息
    GetInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = user.avatar ? process.env.VUE_APP_BASE_API + user.avatar : require("@/assets/images/profile.jpg");
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit("SET_HASPLATFORM")
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          commit('SET_FIELDCODES', user.authFieldCodes)
          commit('SET_CUSTOMSCODE', user.customsCode)
          commit('SET_NICKNAME', user.nickName)
          commit('SET_IDCARDNO', user.idCardNo)
          commit('SET_LOGIN_USER', user)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({commit, state}) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_AVATAR', "")
          commit('REMOVE_PLATFORM_SOURCE')
          commit('REMOVE_PLATFORM_NAME')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
